<?php

namespace App\Dao;

use App\Exceptions\SysExceptions;
use App\Model\UsersUpgradeModel;
use Illuminate\Support\Facades\DB;

class PersonalDao extends BaseDao
{
    /**
     * @throws SysExceptions
     */
    public static function users_upgrade($params = []): array
    {
        //查询用户信息
        $userInfo = UsersDao::builder_user_query([
            'user_id'=>$params['user_id']
        ]);
        if(!$userInfo->exists()){
            self::error('用户不存在');
        }
        $userInfo = $userInfo->first()->toArray();
        $vip_level = $userInfo['vip_level'];
        //最低层数
        $vip_level_index = $vip_level+1;
        $spread_id = $userInfo['spread_id'];
        $index = 1;
        while (true){
            if($spread_id == 0){
                break;
            }
            if($index < $vip_level_index){
                $user_item_info = UsersDao::builder_user_query([
                    'user_id'=>$spread_id
                ])->first()->toArray();
                $spread_id = $user_item_info['spread_id'];
                $index++;
                continue;
            }

            //查询用户信息
            $user_item_info = UsersDao::builder_user_query([
                'user_id'=>$spread_id
            ])->first()->toArray();
            if($user_item_info['vip_level'] >= $vip_level_index){
                break;
            }else{
                $spread_id = $user_item_info['spread_id'];
            }

        }
        //查询用户信息
        return UsersDao::builder_user_query([
            'user_id'=>$spread_id
        ])->first()->toArray();
    }

    /**
     * @throws SysExceptions
     */
    public static function users_upgrade_confirm(array $params)
    {
        $userInfo = UsersDao::builder_user_query([
            'user_id'=>$params['user_id']
        ])->first()->toArray();

        if($userInfo['vip_level'] >= 13){
            self::error('已经达到最高等级');
        }
        //自动匹配人员
        $user_urgrade_info = self::users_upgrade([
            'user_id'=>$params['user_id']
        ]);
        //用户信息
        $users_urgrade_data = UsersUpgradeModel::query()
            ->where('user_id',$params['user_id'])
            ->where('status',0);
        if($users_urgrade_data->exists()){
            return $users_urgrade_data->with('bUserInfo')->first()->toArray();
        }

        if(UsersUpgradeModel::query()->create([
            'user_id'=>$params['user_id'],
            'b_user_id'=>$user_urgrade_info['user_id'],
             'vip_level'=>$userInfo['vip_level']+1
        ]) === false){
            self::error('申请失败');
        }
        return $users_urgrade_data->with('bUserInfo')->first()->toArray();
    }

    public static function user_urgrade_lists(array $params)
    {
        $lists = UsersUpgradeModel::query()
            ->where('b_user_id',$params['b_user_id']);
        $list =  $lists->with('userInfo')->orderBy('status')->orderByDesc('created_at')
            ->get()->toArray();

        foreach ($list as &$value){
            $value['time_str'] = date('Y-m-d H:i:s',$value['created_at']);
        }
        return $list;
    }

    /**
     * @throws SysExceptions
     */
    public static function agree_user_urgrade(array $params)
    {
        checkForData(
            ['users_upgrade_id','status'],
            ['请选择操作','请选择状态'],
            $params
        );
        $info = UsersUpgradeModel::query()
            ->where('users_upgrade_id',$params['users_upgrade_id'])
            ->where('status',0);
        if(!$info->exists()){
            self::error('信息不存在');
        }
        $info = $info->first()->toArray();
        if($params['status'] == 1){
            $uuInfo = UsersDao::builder_user_query([
                'user_id'=>$info['user_id']
            ])->first()->toArray();
            //给用户升级并且扣除一个金币
            $userInfo = UsersDao::builder_user_query([
                'user_id'=>$info['b_user_id']
            ])->first()->toArray();
            if($userInfo['score'] < 1){
                self::error('金币不足');
            }
        }
        DB::beginTransaction();
        try {
            if($params['status'] == 1){
                BillsScoreDao::moneyUpdate($userInfo['user_id'],-1,'用户'.($info['user_id']).'升级扣除');
                //给用户升级
                UsersDao::builder_user_query([
                    'user_id'=>$uuInfo['user_id']
                ])->increment('vip_level',1);
            }
            UsersUpgradeModel::query()->where('users_upgrade_id',$params['users_upgrade_id'])->update([
                'status'=>$params['status'],
                'audit_time'=>time()
            ]);
            DB::commit();
        }catch (\Exception $exception){
            DB::rollBack();
            self::error('升级失败');
        }
    }
}
